/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * spline.c
 *
 * Code generation for function 'spline'
 *
 */

/* Include files */
#include "spline.h"
#include "rt_nonfinite.h"
#include <string.h>

/* Function Definitions */
void spline(const double x[101], const double y[101], double output_breaks[101],
            double output_coefs[400])
{
  double md[101];
  double s[101];
  double dvdf[100];
  double dx[100];
  double d;
  double d31;
  double dnnm2;
  double r;
  int k;
  for (k = 0; k < 100; k++) {
    d = x[k + 1] - x[k];
    dx[k] = d;
    dvdf[k] = (y[k + 1] - y[k]) / d;
  }
  d31 = x[2] - x[0];
  dnnm2 = x[100] - x[98];
  s[0] =
      ((dx[0] + 2.0 * d31) * dx[1] * dvdf[0] + dx[0] * dx[0] * dvdf[1]) / d31;
  s[100] = ((dx[99] + 2.0 * dnnm2) * dx[98] * dvdf[99] +
            dx[99] * dx[99] * dvdf[98]) /
           dnnm2;
  md[0] = dx[1];
  md[100] = dx[98];
  for (k = 0; k < 99; k++) {
    r = dx[k + 1];
    d = dx[k];
    s[k + 1] = 3.0 * (r * dvdf[k] + d * dvdf[k + 1]);
    md[k + 1] = 2.0 * (r + d);
  }
  r = dx[1] / md[0];
  md[1] -= r * d31;
  s[1] -= r * s[0];
  for (k = 0; k < 98; k++) {
    r = dx[k + 2] / md[k + 1];
    md[k + 2] -= r * dx[k];
    s[k + 2] -= r * s[k + 1];
  }
  r = dnnm2 / md[99];
  md[100] -= r * dx[98];
  s[100] -= r * s[99];
  s[100] /= md[100];
  for (k = 98; k >= 0; k--) {
    s[k + 1] = (s[k + 1] - dx[k] * s[k + 2]) / md[k + 1];
  }
  s[0] = (s[0] - d31 * s[1]) / md[0];
  memcpy(&output_breaks[0], &x[0], 101U * sizeof(double));
  for (k = 0; k < 100; k++) {
    double dzzdx;
    d = dvdf[k];
    d31 = s[k];
    dnnm2 = dx[k];
    dzzdx = (d - d31) / dnnm2;
    r = (s[k + 1] - d) / dnnm2;
    output_coefs[k] = (r - dzzdx) / dnnm2;
    output_coefs[k + 100] = 2.0 * dzzdx - r;
    output_coefs[k + 200] = d31;
    output_coefs[k + 300] = y[k];
  }
}

/* End of code generation (spline.c) */
